package com.mj.haier.calc.client;

import com.mj.haier.calc.config.ApplicationProperties;
import com.mj.haier.calc.security.SecurityUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class UserFeignClientInterceptor implements RequestInterceptor {
    private static final String AUTHORIZATION_HEADER = "Authorization";
    private static final String BEARER = "Bearer";

    @Autowired
    private ApplicationProperties applicationProperties;

    @Override
    public void apply(RequestTemplate template) {

        SecurityUtils.getCurrentUserJWT()
                .ifPresent(s -> template.header(AUTHORIZATION_HEADER,String.format("%s %s", BEARER, s)));
        // Optional<String> currentUserJWT = SecurityUtils.getCurrentUserJWT();
        // if(currentUserJWT.isPresent()){
        //     SecurityUtils.getCurrentUserJWT()
        //         .ifPresent(s -> template.header(AUTHORIZATION_HEADER,String.format("%s %s", BEARER, s)));
        // }else {
        //     template.header(AUTHORIZATION_HEADER,String.format("%s %s", BEARER, applicationProperties.getFeign().getToken()));
        // }
    }
}
